package com.example.administrator.u17.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.u17.R;
import com.example.administrator.u17.activity.GameDetailActivity;
import com.example.administrator.u17.model.MineGame;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/11/15.
 */

public class MineGameAdapter extends BaseAdapter {
    private ArrayList<MineGame.DataBean.ReturnDataBean.ItemListBean> mList;
    private Context context;

    public MineGameAdapter(ArrayList<MineGame.DataBean.ReturnDataBean.ItemListBean> mList, Context context) {
        this.mList = mList;
        this.context = context;
    }


    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.mine_game_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Picasso.with(context).load(mList.get(position).getCoverUrl()).into(holder.imageView);

        if (TextUtils.isEmpty(mList.get(position).getTitle())) {
            holder.tvGameName.setText("无名子");
        } else {
//
            holder.tvGameName.setText(mList.get(position).getTitle());
        }

        float size = mList.get(position).getSize() / 1024 + mList.get(position).getSize() % 1024;
        holder.tvNeiCun.setText(size + "MB");

        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, GameDetailActivity.class);
                int appId=mList.get(position).getAppId();
                intent.putExtra("appId",appId);
                context.startActivity(intent);
            }
        });
        Log.d("TAG", "getView: "+mList.size());
        Toast.makeText(context, ""+mList.size(), Toast.LENGTH_SHORT).show();
        // TODO: 2016/11/15  详情
        String detail = mList.get(position).getDesc();
        holder.tvDownLoadNum.setText(mList.get(position).getDownloadTimes() + "次下载");
        holder.tvCosPlay.setText(mList.get(position).getGameType());
        return convertView;
    }

    public static class ViewHolder {
        private TextView tvGameName, tvDownLoadNum, tvNeiCun, tvCosPlay;
        private ImageView imageView;

        public ViewHolder(View itemView) {
            tvCosPlay = (TextView) itemView.findViewById(R.id.tv_mine_game_cosPlay);
            tvDownLoadNum = (TextView) itemView.findViewById(R.id.tv_mine_game_comment);
            tvGameName = (TextView) itemView.findViewById(R.id.tv_mine_game_title);
            tvNeiCun = (TextView) itemView.findViewById(R.id.tv_mine_game_NeiCun);
            imageView = (ImageView) itemView.findViewById(R.id.iv_mine_game);
        }
    }

}
